<?php
class ExcelComponent extends Object{
	
	function createExcel($file_name,$col_name,$data){
		
		$col_orders=array();
		
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:filename=".$file_name.".xls");
		
		$this->printHead();
		$this->printData($col_name,$data);
		$this->printFoot();
	}
	
	function printData($col_name,$data){
		echo "<Worksheet ss:Name=\"Table1\">\n";
		echo "<Table ss:ExpandedColumnCount=\"".count($col_name)."\" ss:ExpandedRowCount=\"".(count($data)+1)."\" x:FullColumns=\"1\"\n";
		echo "   x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\">\n";
		echo "<Column ss:Index=\"3\" ss:AutoFitWidth=\"0\" ss:Width=\"110.25\"/>\n";
		echo "<Row>\n";
		foreach($col_name as $key=>$val){
			$col_orders[]= split("/",$key);
			echo "<Cell ss:StyleID=\"s22\"><Data ss:Type=\"String\">".$val."</Data></Cell>\n";
		}
		echo "</Row>\n";

		foreach($data as $value){
			echo "<Row>\n";
			foreach($col_orders as $c){
				echo "<Cell ss:StyleID=\"s21\"><Data ss:Type=\"String\">".nl2br($this->filter($value[$c['0']][$c['1']]))."</Data></Cell>\n";
			}
			echo "</Row>\n";
		}	
		echo "</Table>\n";
		echo "</Worksheet>\n";
	}
	
	function printHead(){
		echo "<?xml version=\"1.0\"?>\n";
		echo "<?mso-application progid=\"Excel.Sheet\"?>\n";
		echo "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n";
		echo " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n";
		echo " xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n";
		echo " xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\n";
		echo " xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n";
		echo "<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">\n";
		echo "<Version>11.5606</Version>\n";
		echo "</DocumentProperties>\n";
		echo "<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n";
		echo "<WindowHeight>10005</WindowHeight>\n";
		echo "<WindowWidth>10005</WindowWidth>\n";
		echo "<WindowTopX>120</WindowTopX>\n";
		echo "<WindowTopY>135</WindowTopY>\n";
		echo "<ProtectStructure>False</ProtectStructure>\n";
		echo "<ProtectWindows>False</ProtectWindows>\n";
		echo "</ExcelWorkbook>\n";
		echo "<Styles>\n";
		echo "<Style ss:ID=\"Default\" ss:Name=\"Normal\">\n";
		echo "<Alignment ss:Vertical=\"Center\"/>\n";
		echo "<Borders/>\n";
		echo "<Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\"/>\n";
		echo "<Interior/>\n";
		echo "<NumberFormat/>\n";
		echo "<Protection/>\n";
		echo "</Style>\n";
		echo "<Style ss:ID=\"s21\">\n";
		echo "<Borders>\n";
		echo "<Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>\n";
		echo "<Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>\n";
		echo "<Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>\n";
		echo "<Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>\n";
		echo "</Borders>\n";
		echo "</Style>\n";
		echo "<Style ss:ID=\"s22\">\n";
		echo "<Borders>\n";
		echo "<Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>\n";
		echo "<Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>\n";
		echo "<Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>\n";
		echo "<Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>\n";
		echo "</Borders>\n";
		echo "<Interior ss:Color=\"#FFFF99\" ss:Pattern=\"Solid\"/>\n";
		echo "</Style>\n";
		echo "</Styles>\n";
	}
	
	function printFoot(){
		echo "</Workbook>\n";
	}
	
	function filter($data) {
		$search = array (
			'@&@',
			'@"@',
			'@\'@',
			'@<@',
			'@>@'
		); // evaluate as php

		$replace = array (
			'&amp;',
			'&quot;',
			'&#039;',
			'&lt;',
			'&gt;'
		);
		if (is_array($data)) {
			$filtered = array ();
			foreach ($data as $key => $value) {
				$data[$key] = preg_replace($search, $replace, $value);
			}
		} else {
			$data = preg_replace($search, $replace, $data);
		}
		return $data;
	}
}
?>